package com.project.sianet.data.HttpClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.project.sianet.R;
import com.project.sianet.data.Client.ProfesorClient;
import com.project.sianet.entidades.Profesor;
import com.project.sianet.util.SessionManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


public class ProfesorHttpClient implements ProfesorClient {

	private final Context context;
    private final HttpClient httpClient;
//    private final SessionManager session;
    
    public ProfesorHttpClient(Context context) {
    	
    	this.context = context;
//    	this.session = new SessionManager(context);
        
        BasicHttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, false);
        httpClient = new DefaultHttpClient(params);
    	
    }
    
    @Override
	public List<Profesor> getAllByAlumno(String login, String codfam) {
    	
		List<Profesor> result = new ArrayList<Profesor>();
		String content = get("Profesor_getAllByAlumno/" + login + "/" + codfam);
		
		if (content != null) {
			
			try {
				JSONTokener tokener = new JSONTokener(content);
				JSONArray array = (JSONArray) tokener.nextValue();
				for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    Profesor profe = new Profesor();
                    profe.setLogin(obj.isNull("Login") ? null : obj.getString("Login"));
                    profe.setNombres(obj.isNull("Nombres") ? null : obj.getString("Nombres"));
                    profe.setApePaterno(obj.isNull("ApellidoPaterno") ? null : obj.getString("ApellidoPaterno"));
                    profe.setApeMaterno(obj.isNull("ApellidoMaterno") ? null : obj.getString("ApellidoMaterno"));
                    profe.setCodigoAnterior(obj.isNull("CodigoAnterior") ? null : obj.getString("CodigoAnterior"));
                    profe.setCursos(obj.isNull("Cursos") ? null : obj.getString("Cursos"));
                    profe.setHorarioAtencion(obj.isNull("HorarioAtencion") ? null : obj.getString("HorarioAtencion"));
                    profe.setProfesion(obj.isNull("Profesion") ? null : obj.getString("Profesion"));
                    result.add(profe);
                }
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
		}
		
		return result;
	}
	
	private String get(String path) {
        try {
            HttpGet request = new HttpGet(getRequestURI(path));
            request.setHeader("Authorization", SessionManager.getToken());
            HttpResponse res = httpClient.execute(request);
            String content = EntityUtils.toString(res.getEntity());
            return content;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private URI getRequestURI(String path) throws URISyntaxException {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
//        String hostKey = context.getString(R.string.pref_host_key);
//        String host = prefs.getString(hostKey, context.getString(R.string.pref_host_default));
        String host = context.getString(R.string.pref_host_default) + prefs.getString("ColegioPF", "");
        //String portKey = context.getString(R.string.pref_port_key);
        //String port = prefs.getString(portKey, context.getString(R.string.pref_port_default));
        URI requestURI = new URI(host + "/MobileService.svc/" + path);
        //Log.i(LOG_TAG, "requestURI: " + requestURI);
        return requestURI;
    }


}
